{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "gpus = tf.config.list_physical_devices(\"GPU\")\n",
    "\n",
    "if gpus:\n",
    "    tf.config.experimental.set_memory_growth(gpus[0], True)  #设置GPU显存用量按需使用\n",
    "    tf.config.set_visible_devices([gpus[0]],\"GPU\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas            as pd\n",
    "import tensorflow        as tf  \n",
    "import numpy             as np\n",
    "import matplotlib.pyplot as plt\n",
    "# 支持中文\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签\n",
    "plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号\n",
    "\n",
    "from numpy                 import array\n",
    "from sklearn               import metrics\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from keras.models          import Sequential\n",
    "from keras.layers          import Dense,LSTM,Bidirectional\n",
    "\n",
    "\n",
    "# 确保结果尽可能重现\n",
    "from numpy.random          import seed\n",
    "seed(1)\n",
    "tf.random.set_seed(1)\n",
    "\n",
    "# 设置相关参数\n",
    "n_timestamp  = 5    # 时间戳\n",
    "n_epochs     = 30    # 训练轮数\n",
    "# ====================================\n",
    "#      选择模型：\n",
    "#            1: 单层 LSTM\n",
    "#            2: 多层 LSTM\n",
    "#            3: 双向 LSTM\n",
    "# ====================================\n",
    "model_type = 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    year    CPI\n",
      "0   1985  109.3\n",
      "1   1986  106.5\n",
      "2   1987  107.3\n",
      "3   1988  118.8\n",
      "4   1989  118.0\n",
      "5   1990  103.1\n",
      "6   1991  103.4\n",
      "7   1992  106.4\n",
      "8   1993  114.7\n",
      "9   1994  124.1\n",
      "10  1995  117.1\n",
      "11  1996  108.3\n",
      "12  1997  102.8\n",
      "13  1998   99.2\n",
      "14  1999   98.6\n",
      "15  2000  100.4\n",
      "16  2001  100.7\n",
      "17  2002   99.2\n",
      "18  2003  101.2\n",
      "19  2004  103.9\n",
      "20  2005  101.8\n",
      "21  2006  101.5\n",
      "22  2007  104.8\n",
      "23  2008  105.9\n",
      "24  2009   99.3\n",
      "25  2010  103.3\n",
      "26  2011  105.4\n",
      "27  2012  102.6\n",
      "28  2013  102.6\n",
      "29  2014  102.0\n",
      "30  2015  101.4\n",
      "31  2016  102.0\n",
      "32  2017  101.6\n",
      "33  2018  102.1\n",
      "34  2019  102.9\n",
      "35  2020  102.5\n"
     ]
    }
   ],
   "source": [
    "data = pd.read_csv('./datasets/CPI指数.csv')  # 读取股票文件\n",
    "print(data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1985]\n",
      " [1986]\n",
      " [1987]\n",
      " [1988]\n",
      " [1989]]\n",
      "[[109.3]\n",
      " [106.5]\n",
      " [107.3]\n",
      " [118.8]\n",
      " [118. ]]\n",
      "[[2011]\n",
      " [2012]\n",
      " [2013]\n",
      " [2014]\n",
      " [2015]]\n",
      "[[105.4]\n",
      " [102.6]\n",
      " [102.6]\n",
      " [102. ]\n",
      " [101.4]]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "前(35-10=25)天的开盘价作为训练集,10\n",
    "\"\"\"\n",
    "training_count = 25\n",
    "training_set_x = data.iloc[0:training_count, 0:1].values  \n",
    "training_set_y = data.iloc[0:training_count, 1:2].values  \n",
    "test_set_x     = data.iloc[training_count+1:, 0:1].values\n",
    "test_set_y     = data.iloc[training_count+1:, 1:2].values\n",
    "\n",
    "print(training_set_x[0:5])\n",
    "print(training_set_y[0:5])\n",
    "print(test_set_x[0:5])\n",
    "print(test_set_y[0:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {},
   "outputs": [],
   "source": [
    "#将数据归一化，范围是0到1\n",
    "sc  = MinMaxScaler(feature_range=(0, 1))\n",
    "training_set_scaled_x = sc.fit_transform(training_set_x)\n",
    "training_set_scaled_y = sc.fit_transform(training_set_y)\n",
    "testing_set_scaled_x  = sc.transform(test_set_x) \n",
    "testing_set_scaled_y  = sc.transform(test_set_y)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5, 5, 1)\n"
     ]
    }
   ],
   "source": [
    "# 取前 n_timestamp 天的数据为 X；n_timestamp+1天数据为 Y。\n",
    "def data_split(sequence_x, sequence_y, n_timestamp):\n",
    "    X = []\n",
    "    y = []\n",
    "    for i in range(len(sequence_y)):\n",
    "        end_ix = i + n_timestamp\n",
    "        \n",
    "        if end_ix > len(sequence_y)-1:\n",
    "            break\n",
    "            \n",
    "        seq_x, seq_y = sequence_x[i:end_ix], sequence_y[i:end_ix]\n",
    "        X.append(seq_x)\n",
    "        y.append(seq_y)\n",
    "    return array(X), array(y)\n",
    "\n",
    "X_train, y_train = data_split(training_set_scaled_x,training_set_scaled_y, n_timestamp)\n",
    "X_train          = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)\n",
    "\n",
    "X_test, y_test   = data_split(testing_set_scaled_x, testing_set_scaled_y, n_timestamp)\n",
    "X_test           = X_test.reshape(X_test.shape[0], X_test.shape[1], 1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential_9\"\n",
      "_________________________________________________________________\n",
      " Layer (type)                Output Shape              Param #   \n",
      "=================================================================\n",
      " lstm_10 (LSTM)              (None, 50)                10400     \n",
      "                                                                 \n",
      " dense_9 (Dense)             (None, 1)                 51        \n",
      "                                                                 \n",
      "=================================================================\n",
      "Total params: 10,451\n",
      "Trainable params: 10,451\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "# 建构 LSTM模型\n",
    "if model_type == 1:\n",
    "    # 单层 LSTM\n",
    "    model = Sequential()\n",
    "    model.add(LSTM(units=50, activation='relu',\n",
    "                   input_shape=(X_train.shape[1], 1)))\n",
    "    model.add(Dense(units=1))\n",
    "if model_type == 2:\n",
    "    # 多层 LSTM\n",
    "    model = Sequential()\n",
    "    model.add(LSTM(units=50, activation='relu', return_sequences=True,\n",
    "                   input_shape=(X_train.shape[1], 1)))\n",
    "    model.add(LSTM(units=50, activation='relu'))\n",
    "    model.add(Dense(1))\n",
    "if model_type == 3:\n",
    "    # 双向 LSTM\n",
    "    model = Sequential()\n",
    "    model.add(Bidirectional(LSTM(50, activation='relu'),\n",
    "                            input_shape=(X_train.shape[1], 1)))\n",
    "    model.add(Dense(1))\n",
    "    \n",
    "model.summary() # 输出模型结构\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 该应用只观测loss数值，不观测准确率，所以删去metrics选项，一会在每个epoch迭代显示时只显示loss值\n",
    "model.compile(optimizer=tf.keras.optimizers.Adam(0.001),\n",
    "              loss='mean_squared_error')  # 损失函数用均方误差\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/30\n",
      "1/1 [==============================] - 1s 1s/step - loss: 0.1758 - val_loss: 39.5195\n",
      "Epoch 2/30\n",
      "1/1 [==============================] - 0s 29ms/step - loss: 0.1730 - val_loss: 47.3009\n",
      "Epoch 3/30\n",
      "1/1 [==============================] - 0s 39ms/step - loss: 0.1704 - val_loss: 55.3661\n",
      "Epoch 4/30\n",
      "1/1 [==============================] - 0s 32ms/step - loss: 0.1678 - val_loss: 63.6654\n",
      "Epoch 5/30\n",
      "1/1 [==============================] - 0s 38ms/step - loss: 0.1653 - val_loss: 72.1495\n",
      "Epoch 6/30\n",
      "1/1 [==============================] - 0s 36ms/step - loss: 0.1628 - val_loss: 80.7847\n",
      "Epoch 7/30\n",
      "1/1 [==============================] - 0s 30ms/step - loss: 0.1604 - val_loss: 89.5865\n",
      "Epoch 8/30\n",
      "1/1 [==============================] - 0s 27ms/step - loss: 0.1581 - val_loss: 98.9562\n",
      "Epoch 9/30\n",
      "1/1 [==============================] - 0s 28ms/step - loss: 0.1558 - val_loss: 108.4859\n",
      "Epoch 10/30\n",
      "1/1 [==============================] - 0s 28ms/step - loss: 0.1535 - val_loss: 118.2458\n",
      "Epoch 11/30\n",
      "1/1 [==============================] - 0s 29ms/step - loss: 0.1513 - val_loss: 128.1351\n",
      "Epoch 12/30\n",
      "1/1 [==============================] - 0s 27ms/step - loss: 0.1492 - val_loss: 138.2528\n",
      "Epoch 13/30\n",
      "1/1 [==============================] - 0s 26ms/step - loss: 0.1471 - val_loss: 148.7282\n",
      "Epoch 14/30\n",
      "1/1 [==============================] - 0s 27ms/step - loss: 0.1451 - val_loss: 159.4838\n",
      "Epoch 15/30\n",
      "1/1 [==============================] - 0s 66ms/step - loss: 0.1431 - val_loss: 170.3488\n",
      "Epoch 16/30\n",
      "1/1 [==============================] - 0s 82ms/step - loss: 0.1412 - val_loss: 181.6411\n",
      "Epoch 17/30\n",
      "1/1 [==============================] - 0s 32ms/step - loss: 0.1394 - val_loss: 193.5709\n",
      "Epoch 18/30\n",
      "1/1 [==============================] - 0s 30ms/step - loss: 0.1376 - val_loss: 206.0197\n",
      "Epoch 19/30\n",
      "1/1 [==============================] - 0s 48ms/step - loss: 0.1358 - val_loss: 219.0664\n",
      "Epoch 20/30\n",
      "1/1 [==============================] - 0s 35ms/step - loss: 0.1342 - val_loss: 232.6279\n",
      "Epoch 21/30\n",
      "1/1 [==============================] - 0s 34ms/step - loss: 0.1326 - val_loss: 246.8466\n",
      "Epoch 22/30\n",
      "1/1 [==============================] - 0s 49ms/step - loss: 0.1310 - val_loss: 261.8865\n",
      "Epoch 23/30\n",
      "1/1 [==============================] - 0s 44ms/step - loss: 0.1295 - val_loss: 277.9135\n",
      "Epoch 24/30\n",
      "1/1 [==============================] - 0s 32ms/step - loss: 0.1280 - val_loss: 295.0356\n",
      "Epoch 25/30\n",
      "1/1 [==============================] - 0s 31ms/step - loss: 0.1266 - val_loss: 313.6388\n",
      "Epoch 26/30\n",
      "1/1 [==============================] - 0s 30ms/step - loss: 0.1253 - val_loss: 334.0956\n",
      "Epoch 27/30\n",
      "1/1 [==============================] - 0s 28ms/step - loss: 0.1240 - val_loss: 356.3166\n",
      "Epoch 28/30\n",
      "1/1 [==============================] - 0s 29ms/step - loss: 0.1228 - val_loss: 380.5373\n",
      "Epoch 29/30\n",
      "1/1 [==============================] - 0s 30ms/step - loss: 0.1216 - val_loss: 407.0132\n",
      "Epoch 30/30\n",
      "1/1 [==============================] - 0s 30ms/step - loss: 0.1204 - val_loss: 436.0050\n",
      "Model: \"sequential_9\"\n",
      "_________________________________________________________________\n",
      " Layer (type)                Output Shape              Param #   \n",
      "=================================================================\n",
      " lstm_10 (LSTM)              (None, 50)                10400     \n",
      "                                                                 \n",
      " dense_9 (Dense)             (None, 1)                 51        \n",
      "                                                                 \n",
      "=================================================================\n",
      "Total params: 10,451\n",
      "Trainable params: 10,451\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "history = model.fit(X_train, y_train, \n",
    "                    batch_size=64, \n",
    "                    epochs=n_epochs, \n",
    "                    validation_data=(X_test, y_test), \n",
    "                    validation_freq=1)                  #测试的epoch间隔数\n",
    "\n",
    "model.summary()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXgAAAEFCAYAAADpIfy5AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bG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iX+Owh1TVOcorgLeOZeTbx35V4D7zOyHQG9gGvABcDLwK2AFfv/XT4CLnHPnAenAxWVaeCEiPKZwbfn3uK1fdlUXrATHE7fnCMDMOgK18BcM+V/3cOciLs/P0YpxTAl7jsL8XkKCn6MCjimqcxRXAZ+fc+5+YCJwPfCMc24PcB/wc+fcn4GVwDXAYufc5tDD5gHx9E+xY4Q7pgKOMyH2uI3ieOL2HJlZbeBR4FrCv+6RtsWNYh5TIp+jcBL9HIUT1TmKqwMuwEIgA3g4dLsW0MbM0oD2gAOeM7OzQm0/BhYFUGc0FnLsMYVrS6Q9bhdS9PHE5TkyswrAOOAO59w6wr/ukbbFhRIcUyKfo3AS/RyFE905cs7F3Rcw46jv/wRcddTt84Bl+L94U/B/7VoDi4ElwF+Crj/aYyrgOKuHTt7D+KGoGkEfQwmPJy7PEfALIBuYEfq6Ov/rHu5cxPP5KcExJew5KuD3MqHPUQHHFNU50idZ41joXfQewEzn3Jag60kV4V73SNviVaLXn1+ktSfSMZZGrQp4EZEklQhj8CIiUgwKeBGRJKWAFxFJUgp4EZEkpYAXEUlS/w/nWPTZIPDfWAAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x_len = len(history.history['loss'])\n",
    "x_loss = data.iloc[0:x_len,0:1]\n",
    "\n",
    "plt.plot(x_loss, history.history['loss'], label='Training Loss')\n",
    "plt.plot(x_loss, history.history['val_loss'], label='Validation Loss')\n",
    "plt.title('Training and Validation Loss')\n",
    "plt.legend()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[634.077  ]\n",
      " [634.33344]\n",
      " [634.5898 ]\n",
      " [634.8461 ]\n",
      " [635.1024 ]] (5, 5, 1)\n",
      "5\n"
     ]
    },
    {
     "data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gSeAK1o9XuX1K4H5FeujPl4N1NXW8QKOBZ6kdOZ1D6WzrlEfrwbqGo2fry8D547Bn6966mrZeFXZZ0G1cWjWeHX0f/iW79rPBe7P0uWTMdFfs+tqFserMY5XYxyvxtRbV7P327p/J4e/JGl4OuaavySpeQx/SSogw1+SCsjwl2qIiH8dpflb/rH89SPl9mtHuTRpxLzhKw0hIn6Tme8Y7TqkZqprXgpJ20REb5YmmCMiVlOag+X/AQcCfwvcBtxI6T3Zd2TmVaNTqVSbl32kkXkLcBal2R7/gtJ/0y4BvpeZxwMfjoj9R7E+qSrDXxqZF7M0D8yzlGaLDOAw4DMR0Utpwq2DRq88qTov+0jN9zTww8z8WUScQ+0ZNaVR45m/1HxXA1+IiAcpTQ/84ijXI+3Ad/tIUgF55i9JBWT4S1IBGf6SVECGvyQVkOEvSQVk+EtSAf1/tU+KJjFKiqQAAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "predicted_cpi = model.predict(X_test)                        # 测试集输入模型进行预测\n",
    "\n",
    "predicted_cpi = sc.inverse_transform(predicted_cpi)  # 对预测数据还原---从（0，1）反归一化到原始范围\n",
    "print(predicted_cpi,y_test.shape)\n",
    "\n",
    "y_test        = y_test.reshape(y_test.shape[0], y_test.shape[1])\n",
    "real_cpi      = sc.inverse_transform(y_test)# 对真实数据还原---从（0，1）反归一化到原始范围\n",
    "\n",
    "#print(X_test.shape, X_test)\n",
    "x_pre_len = len(predicted_cpi)\n",
    "print(x_pre_len)\n",
    "x_pre = data.iloc[-x_pre_len:,0:1]\n",
    "# 画出真实数据和预测数据的对比曲线\n",
    "plt.plot(x_pre, real_cpi, color='red', label='Real CPI')\n",
    "plt.plot(x_pre, predicted_cpi, color='blue', label='Predicted CPI')\n",
    "plt.title('CPI Prediction')\n",
    "plt.xlabel('Time')\n",
    "plt.ylabel('CPI')\n",
    "plt.legend()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "均方误差: 0.22765\n",
      "均方根误差: 0.47712\n",
      "平均绝对误差: 0.42367\n",
      "R2: -126.94656\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "MSE  ：均方误差    ----->  预测值减真实值求平方后求均值\n",
    "RMSE ：均方根误差  ----->  对均方误差开方\n",
    "MAE  ：平均绝对误差----->  预测值减真实值求绝对值后求均值\n",
    "R2   ：决定系数，可以简单理解为反映模型拟合优度的重要的统计量\n",
    "\n",
    "详细介绍可以参考文章：https://blog.csdn.net/qq_38251616/article/details/107997435\n",
    "\"\"\"\n",
    "MSE   = metrics.mean_squared_error(predicted_cpi, real_cpi)\n",
    "RMSE  = metrics.mean_squared_error(predicted_cpi, real_cpi)**0.5\n",
    "MAE   = metrics.mean_absolute_error(predicted_cpi, real_cpi)\n",
    "R2    = metrics.r2_score(predicted_cpi, real_cpi)\n",
    "\n",
    "print('均方误差: %.5f' % MSE)\n",
    "print('均方根误差: %.5f' % RMSE)\n",
    "print('平均绝对误差: %.5f' % MAE)\n",
    "print('R2: %.5f' % R2)\n"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "a8f61be024eba58adef938c9aa1e29e02cb3dece83a5348b1a2dafd16a070453"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit ('base': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
